// pages/mine/mine.js
import { makePhoneCall } from '../../utils/util'
import { logout } from '../../utils/apis'

const app = getApp()
Page({
  data: {},
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '/pages/login/shouquan'
      })
    }
  },
  onShow: function() {
    var that = this
    if (wx.getStorageSync('loginInfo')) {
      if (!this.data.loginInfo) {
        this.setData({
          loginInfo: wx.getStorageSync('loginInfo') || {}
        })
      }
    }
    if (wx.getStorageSync('userInfo')) {
      if (!this.data.userInfo) {
        this.setData({
          userInfo: wx.getStorageSync('userInfo') || {}
        })
      }
    }
  },
  onLogout(e) {
    var that = this
    var {
      loginInfo: { phone },
      loading
    } = this.data
    if (loading) {
      return
    }
    this.setData({
      loading: true
    })
    logout({
      phone,
      success(data) {
        app.setLoginInfo(data)
        that.setData({
          loginInfo: null,
          loading: false
        })
      }
    })
  },
  callback(loginInfo) {
    this.setData({
      loginInfo: loginInfo.user_info
    })
  },
  onPhoneTap(e) {
    makePhoneCall(e.currentTarget.dataset.phone)
  }
})
